package cn.edu.nju.software.service.impl;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.nju.software.bean.show.BookItemInOrder;
import cn.edu.nju.software.bean.show.OrderSearchListItem;
import cn.edu.nju.software.bean.storage.Order;
import cn.edu.nju.software.bean.storage.User;
import cn.edu.nju.software.dao.intf.OrderBookDao;
import cn.edu.nju.software.dao.intf.OrderDao;
import cn.edu.nju.software.dao.intf.UserDao;
import cn.edu.nju.software.service.intf.OrderService;
import cn.edu.nju.software.util.Constants;

public class OrderServiceImpl implements OrderService {
	
	private OrderDao orderDao;
	private OrderBookDao orderBookDao;
	private UserDao userDao;

	public void setOrderDao(OrderDao orderDao) {
		this.orderDao = orderDao;
	}

	public void setOrderBookDao(OrderBookDao orderBookDao) {
		this.orderBookDao = orderBookDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	@Override
	public Order getOrderById(int id) throws SQLException {
		return orderDao.getOrderById(id);
	}

	@Override
	public List<OrderSearchListItem> searchOrders(int id, String user, int status, Date beginDate, Date endDate, 
			String bookTitle) throws SQLException {
		Map<Integer, Object> criteria = new HashMap<Integer, Object>();
		if (id > 0) {
			criteria.put(Constants.SEARCH_ORDER_ID, id);
		}
		if (user != null && !user.equals("")) {
			criteria.put(Constants.SEARCH_USER, user);
		}
		if (status >= 0) {
			criteria.put(Constants.SEARCH_ORDER_STATUS, status);
		}
		if (beginDate != null || endDate != null) {
			if (beginDate == null) {
				beginDate = new Date(Long.MIN_VALUE);
			}
			if (endDate == null) {
				endDate = new Date(Long.MAX_VALUE);
			}
			List<Date> date = new ArrayList<Date>(2);
			date.add(beginDate);
			date.add(endDate);
			criteria.put(Constants.SEARCH_DATE, date);
		}
		if (bookTitle != null && !bookTitle.equals("")) {
			criteria.put(Constants.SEARCH_TITLE, bookTitle);
		}
		return orderDao.searchOrders(criteria);
	}

	@Override
	public boolean modifyOrderStatus(int orderID, int operateCode) throws SQLException {
		switch (operateCode) {
		case Constants.ORDER_CANCELED:
			return orderDao.cancelOrder(orderID);
		case Constants.ORDER_DISPATCHED:
			return orderDao.dispatchOrder(new Order(orderID, 0, null, 0, null, null, new Timestamp(System.currentTimeMillis()), null));
		case Constants.ORDER_CONFIRMED:
			return orderDao.confirmOrder(new Order(orderID, 0, null, 0, null, null, null, new Timestamp(System.currentTimeMillis())));
		case Constants.ORDER_FAILED:
			return orderDao.failOrder(orderID);
		default:
			return false;
		}
	}

	@Override
	public List<BookItemInOrder> getOrderBooks(int id) throws SQLException {
		return orderBookDao.getOrderBooks(id);
	}

	@Override
	public User getUsersByID(int userID) throws SQLException {
		List<Integer> usersList = new ArrayList<Integer>(1);
		usersList.add(userID);
		return userDao.getUsersByID(usersList).get(userID);
	}

}
